package com.tarry.websocket.comfig;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

/**
 * 开始websocket的支持
 * 广播模式
 */
@Configuration
@EnableWebSocketMessageBroker//启用STOMP协议来传输基于代理（message broker）的消息
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    /**
     * /注册STOMP协议的节点endpoint,并映射到指定的URL
     * @param registry
     */
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        //注册STOMP协议的endpoint，并指定协议为SockJS
        registry.addEndpoint("/endpointWisely").withSockJS();
    }

    /**
     * 配置代理消息
     * @param registry
     */
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        //配置/topic代理
        //使用内置的消息代理进行订阅和广播；路由消息的目标头以“/topic”开头。
        registry.enableSimpleBroker("/topic");
    }
}
